package daikoku;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {

	public static void main(String[] args) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(new File("sena.csv")));

			Map<String, Integer> map = new HashMap<String, Integer>();
			Map<String, Integer> map2 = new HashMap<String, Integer>();

			int count = 0;
			String line = null;
			while ((line = br.readLine()) != null) {
				List<Integer> dezenas = order(line);

				for (int i = 0; i < 6; i++) {
					add(map, String.valueOf(dezenas.get(i)));
				}

				add(map2, dezenas.get(0) + "," + dezenas.get(1));
				add(map2, dezenas.get(2) + "," + dezenas.get(3));
				add(map2, dezenas.get(4) + "," + dezenas.get(5));

				count++;
			}

			System.out.println(count);
			System.out.println(entriesSortedByValues(map));
			System.out.println(entriesSortedByValues(map2));

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void add(Map<String, Integer> map, String key) {
		if (!map.containsKey(key)) {
			map.put(key, 0);
		}
		map.put(key, map.get(key) + 1);
	}

	static <K, V extends Comparable<? super V>> List<Entry<K, V>> entriesSortedByValues(Map<K, V> map) {

		List<Entry<K, V>> sortedEntries = new ArrayList<Entry<K, V>>(map.entrySet());

		Collections.sort(sortedEntries, new Comparator<Entry<K, V>>() {
			@Override
			public int compare(Entry<K, V> e1, Entry<K, V> e2) {
				return e2.getValue().compareTo(e1.getValue());
			}
		});

		return sortedEntries;
	}

	static List<Integer> order(String line) {
		ArrayList<Integer> result = new ArrayList<Integer>();
		String[] tmp = line.split(",");
		for (String n : tmp) {
			result.add(Integer.valueOf(n));
		}
		Collections.sort(result);
		return result;
	}
}
